package audi.json;
import java.util.logging.Logger;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import audi.model.GetTag;
import audi.model.LocalData;
import audi.model.OJChangepass;
import audi.model.OJLogin;

public class  GetJson {
	public String TAG = "tag";
	public String StrJson = "strjson";
	private static Logger logtag = Logger.getLogger(GetJson.class.getName());
	public GetTag getTag(String json)
	
	{	
		GetTag result = new GetTag();
		JSONParser jsonParser = new JSONParser();
		logtag.info("tag-2"+json);
		if (json!=null)
		{
			
        try {       	
			JSONObject jsonObject = (JSONObject) jsonParser.parse(json);			
			String mTag = (String) jsonObject.get(TAG);
			String mStrjson = (String) jsonObject.get(StrJson).toString();
			result.setTag(mTag);
			result.setJson(mStrjson);
			try{
			result.setGroup((String) jsonObject.get(LocalData.TAG_USERGROUP).toString());
			}		
			catch(Exception e)
			{
//				result.setGroup("All");
			}
			result.setBox((Boolean) jsonObject.get("Box"));
			logtag.info("\nGetTag thành công" + "TAG: "+ result.getTag() +" -- JSONstring: "+ result.getJson() +"---Group: "+result.getGroup());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			logtag.fine("\nLỗi trong gettag"+e);
		}
		}		
		else logtag.fine("\nChuoi json null");
		
		return result;
	}
	public OJLogin getlogin(String json)
	{
		OJLogin result = new OJLogin();
		JSONParser jsonParser = new JSONParser();
		if (json!=null)
		{
        try {
			JSONObject jsonObject = (JSONObject) jsonParser.parse(json);
			String username = (String) jsonObject.get(LocalData.TAG_USERNAME);
			String password = (String) jsonObject.get(LocalData.TAG_PASS);
			String group = (String) jsonObject.get(LocalData.TAG_USERGROUP);
			result.setUsername(username);
			result.setPassword(password);
			result.setGroup(group);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		else System.out.print("MainJsonOJ getTag: Json Null" );
		return result;
	}
	public OJChangepass getchangepass(String json)
	{
		OJChangepass result = new OJChangepass();
		JSONParser jsonParser = new JSONParser();
		if (json!=null)
		{
        try {
			JSONObject jsonObject = (JSONObject) jsonParser.parse(json);
			String username = (String) jsonObject.get(LocalData.TAG_USERNAME);
			String password = (String) jsonObject.get(LocalData.TAG_PASS);
			String newpass = (String) jsonObject.get(LocalData.TAG_NEWPASS);
			result.setUsername(username);
			result.setOldpass(password);
			result.setNewpass(newpass);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		else System.out.print("MainJsonOJ getTag: Json Null" );
		return result;
	}
}
